#include<bits/stdc++.h>
#define int long long 
using namespace std;
int n,ans;
int a[25];
int vis[25];
vector<int>to[25];
void dfs(int t,int x,int sum){
	sum+=a[x];
	if(t==n){
		ans=max(ans,sum);
		return;
	}
	for(int i=0;i<to[x].size();i++){
		int y=to[x][i];
		a[y]+=a[x];
	}
	for(int y=1;y<=n;y++){
		if(vis[y]==0){
			vis[y]=1;
			dfs(t+1,y,sum);
			vis[y]=0;
		}
	}
	for(int i=0;i<to[x].size();i++){
		int y=to[x][i];
		a[y]-=a[x];
	}
}
signed main(){
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		to[u].push_back(v);
		to[v].push_back(u);
	}
	dfs(0,0,0);
	cout<<ans;
	return 0; 
} 
